package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfbapi.FormHelper;
import com.atguigu.srb.core.hfbapi.HfbConst;
import com.atguigu.srb.core.hfbapi.RequestHelper;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author Mr.xu
 * @since 2023-04-06
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public String commitBind(UserBindVO userBindVO, String userId) {
        UserBind userBind = new UserBind();
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id", userId);
        UserBind userBindDb = baseMapper.selectOne(userBindQueryWrapper);
        if (userBindDb == null) {
            userBind.setBindCode(userBindVO.getBankNo());
            userBind.setName(userBindVO.getName());
            userBind.setMobile(userBindVO.getMobile());
            userBind.setBankType(userBindVO.getBankType());
            userBind.setBankNo(userBindVO.getBankNo());
            userBind.setIdCard(userBindVO.getIdCard());
            baseMapper.insert(userBind);
        } else {
            BeanUtils.copyProperties(userBindVO, userBindDb);
            baseMapper.updateById(userBindDb);
        }

        // 生成汇付宝api接口,生成汇付宝表单
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard", userBindVO.getIdCard());
        paramMap.put("personalName", userBindVO.getName());
        paramMap.put("bankType", userBindVO.getBankType());
        paramMap.put("bankNo", userBindVO.getBankNo());
        paramMap.put("mobile", userBindVO.getMobile());
        paramMap.put("return_url", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notify_url", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        String form = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
        return form;
    }

    @Override
    public void commitBindNotify(Map<String, Object> parameterMap) {
        String bindCode = (String) parameterMap.get("bindCode");
        String agentUserId = (String) parameterMap.get("agentUserId");

        // 更新user_bind信息
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper();
        userBindQueryWrapper.eq("user_id",agentUserId);
        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);

        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        baseMapper.updateById(userBind);

        // 更新user_info信息
        UserInfo userInfo = userInfoMapper.selectById(agentUserId);
        userInfo.setName(userBind.getName());
        userInfo.setNickName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(userBind.getStatus());
        userInfo.setBindCode(userBind.getBindCode());
        userInfoMapper.updateById(userInfo);

    }
}
